from django.http import JsonResponse
from django.shortcuts import render


def chart_list(request):
    return render(request, 'chart_list.html')


def chart_line(request):
    """构造柱状图的数据"""
    # 数据可以去数据库中获取
    legend = ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']

    series_list = [
        {
            'name': 'Email',
            'type': 'line',
            'stack': 'Total',
            'areaStyle': {},
            'emphasis': {
                'focus': 'series'
            },
            'data': [120, 132, 101, 134, 90, 230, 210]
        },
        {
            'name': 'Union Ads',
            'type': 'line',
            'stack': 'Total',
            'areaStyle': {},
            'emphasis': {
                'focus': 'series'
            },
            'data': [220, 182, 191, 234, 290, 330, 310]
        },
        {
            'name': 'Video Ads',
            'type': 'line',
            'stack': 'Total',
            'areaStyle': {},
            'emphasis': {
                'focus': 'series'
            },
            'data': [150, 232, 201, 154, 190, 330, 410]
        },
        {
            'name': 'Direct',
            'type': 'line',
            'stack': 'Total',
            'areaStyle': {},
            'emphasis': {
                'focus': 'series'
            },
            'data': [320, 332, 301, 334, 390, 330, 320]
        },
        {
            'name': 'Search Engine',
            'type': 'line',
            'stack': 'Total',
            'label': {
                'show': 'true',
                'position': 'top'
            },
            'areaStyle': {},
            'emphasis': {
                'focus': 'series'
            },
            'data': [820, 932, 901, 934, 1290, 1330, 1320]
        }
    ]

    x_axis = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

    result = {
        'status': True,
        'data': {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis
        }
    }
    return JsonResponse(result)


def chart_bar(request):
    """构造柱状图的数据"""
    # 数据可以去数据库中获取
    legend = ['销量', '业绩']

    series_list = [
        {
            'name': '销量',
            'type': 'bar',
            'data': [5, 20, 36, 10, 10, 20]
        },
        {
            'name': '业绩',
            'type': 'bar',
            'data': [6, 22, 34, 11, 12, 25]
        }
    ]

    x_axis = ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']

    result = {
        'status': True,
        'data': {
            'legend': legend,
            'series_list': series_list,
            'x_axis': x_axis
        }
    }
    return JsonResponse(result)


def chart_pie(request):
    """构建饼图的数据"""

    db_data_list = [
        {'value': 1048, 'name': 'Search Engine'},
        {'value': 735, 'name': 'Direct'},
        {'value': 580, 'name': 'Email'},
        {'value': 484, 'name': 'Union Ads'},
        {'value': 300, 'name': 'Video Ads'}
    ]

    result = {
        'status': True,
        'data': db_data_list
    }
    return JsonResponse(result)


def highcharts(request):
    return render(request, 'highcharts.html')